﻿<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>ASP.NET SignalR Stock Ticker</title>
    <style type="text/css">
        #msgcontent {
            width: 96%;
            height: 400px;
            border: 2px solid blue;
            padding: 5px;
            margin: 5px 0px;
            overflow-x: hidden;
            overflow-y: auto;
        }

        .button {
            height: 100px;
            padding: 15px 12px;
            display: inline-block;
            color: white;
            margin-left: 10px;
            background-color: #4CAF50;
            border-radius: 8px;
            text-align: center;
        }
    </style>
</head>
<body>
    <script src="@SiteUrl.GetStaticResourceScriptUrl("~/js/jquery/jquery-2.1.1.min.js")"></script>
    <script src="@SiteUrl.GetStaticResourceScriptUrl("~/js/signalr/jquery.signalR-2.4.3.js")"></script>
    <script src="http://localhost:55144/signalr/hubs"></script>
   
    <div class="mypop" id="liaotian" style="display: block">
        <div class="graybox"></div>
        <div class="popbox">
            <div class="poptop">
                <h3>匿名聊天室</h3>
                <a href="#" class="close"></a>
            </div>
            <div class="popcon">
                <div>
                    <div class="group">
                        <input type="text" id="nickname" value="请输入昵称" onfocus="if (value == '请输入昵称') { value = '' }" onblur="    if (value == '') { value = '请输入昵称' }">
                        <i class="fa fa-user"></i>
                    </div>

                </div>

            </div>
            <div class="divbtn clearfix">
                <a class="btn" onclick="quxiao()">取消</a>
                <a href="#" class="btn" onclick="GoHome()">进入聊天室</a>
            </div>
        </div>
    </div>



    <div id="msg" style="margin:10px;">
        <h3>欢迎<font color='blue' style="font-family: Glyphicons Halflings" size="3" id="nick"></font>进入聊天室</h3>
        <div id="msgcontent">
        </div>
        <div>

        </div>
        <div style="text-align: left; width: 98%; margin: 10px auto 0;zoom:1;">
            <textarea id="message" rows=5 style="width:90%;border:1px solid #271818;height:98px;float:left;"></textarea>
            <input class="button" type="button" value="发送消息" id="btnSend" />
        </div>

    </div>
    <script type="text/javascript">
        var clientName = "";

        var eUsers = $("#users");
        var hubUrl = 'http://localhost:55144/signalr'; //服务端ip

        $(function () {
            //绑定输入框，这里只能 是ID
            $("#btnSend").keydown(function (event) {
                event = document.all ? window.event : event;
                if ((event.keyCode || event.which) == 13) {
                    var msg = $("#message").val();

                    //$.get('http://localhost:55144/api/notice/getmsg/' + clientName + '/' + msg + '', {

                    //}, function (res) {
                    //    // alert("suc" + msg);
                    //    $("#message").val("");
                    //    $("#message").focus();
                    //    }, 'text');

                    //$.ajax({
                    //    url: 'http://localhost:55144/api/notice/getmsg/' + clientName + '/' + msg + '',
                    //    //data: { newpwd: newpwd, renewpwd: renewpwd },
                    //    type: "get",
                    //    dataType: "json",
                    //    success: function (data) {
                    //        $("#message").val("");
                    //        $("#message").focus();
                    //    }
                    //});

                    $.ajax({
                        url: 'http://localhost:55144/api/notice/SendNoticeToUser',
                        data: {
                            users: ["000000", "0376"], message: "病历质控消息测试", data: {} },
                        type: "post",
                        dataType: "json",
                        success: function (data) {
                            $("#message").val("");
                            $("#message").focus();
                        }
                    });
                }
            });
        });

        //初始化服务端和客户端的连接
        function initTalk() {
            document.getElementById('nick').innerHTML = clientName;
            $.connection.hub.url = hubUrl;
            $.connection.hub.qs = { 'user' : '@ViewBag.UserCode' ,'orgid':'@ViewBag.OrganizeId'};
            var hub = $.connection.noticeHub;
            hub.client.showMessage = function (msg) {
                var html = "<div id='conheight'><font size='8' color='blue'>" + msg.split(',')[0] + "          " + msg.split(',')[2] + "</font></br>" + msg.split(',')[1] + "</div>";
                $("#msgcontent").append(html);
                var conheight = document.getElementById("conheight").clientHeight;
                console.log(conheight);
                if (conheight > 400) {
                    document.getElementById("msgcontent").scrollTop = document.getElementById("conheight").scrollHeight;
                }
            }


            $.connection.hub.start().done(function () {

                $("#btnSend").click(function () {
                    //var msg = $("#message").val();

                    //$.get('http://localhost:55144/api/notice/getmsg/' + clientName + '/' + msg + '', {

                    //}, function (res) {
                    //    // alert("suc" + msg);
                    //    $("#message").val("");
                    //    $("#message").focus();
                    //}, 'text');

                    $.ajax({
                        url: 'http://localhost:55144/api/notice/SendNoticeToUser',
                        data: {
                            user: "000000", message: "病历质控消息测试", orgid:"6d5752a7-234a-403e-aa1c-df8b45d3469f"
                        },
                        type: "post",
                        dataType: "json",
                        success: function (data) {
                            $("#message").val(data);
                            $("#message").focus();
                        }
                    });

                });


            });
        }
        //登录方法，登录成功以后调用 initTalk()进行$.connection服务端的连接
        function GoHome() {
            clientName = $("#nickname").val();
            if (clientName.length == 0 || clientName == "请输入昵称") {
                alert("随便起个名字吧，我们不会记录");
                return;
            }

            document.getElementById("liaotian").style.display = "none";
            initTalk();
        }

        function randomString() {

            var $chars = 'ycz123';    /****默认去掉了容易混淆的字符oOLl,9gq,Vv,Uu,I1****/
            var maxPos = $chars.length;
            var pwd = '';
            for (i = 0; i < 3; i++) {
                pwd += $chars.charAt(Math.floor(Math.random() * maxPos));
            }
            return pwd;
        }
        function quxiao() {
            window.open(location, '_self').close();
        }

    </script>
</body>
</html>